package com.pandy.leetcode;

/**
 * @description: 计算二叉树深度
 *
 * https://leetcode.cn/problems/er-cha-shu-de-shen-du-lcof/description/
 * @author: panhong
 * @date: 2025/4/2
 */
public class Code006_BinaryTree_Height {

    public static int height(BinaryTreeNode head) {
        return process(head).height;
    }

    static class Info {
        private int height;

        public Info(int height) {
            this.height = height;
        }

        public int getHeight() {
            return height;
        }
    }

    public static Info process(BinaryTreeNode x) {
        if(x == null) {
            return new Info(0);
        }

        Info leftInfo = process(x.left);
        Info rightInfo = process(x.right);

        int height = Math.max(leftInfo.height, rightInfo.height) + 1;
        return new Info(height);
    }

    public static void main(String[] args) {
        BinaryTreeNode n1 = new BinaryTreeNode(1);
        BinaryTreeNode n2 = new BinaryTreeNode(2);
        BinaryTreeNode n3 = new BinaryTreeNode(3);
        BinaryTreeNode n4 = new BinaryTreeNode(4);
        BinaryTreeNode n5 = new BinaryTreeNode(5);


        BinaryTreeNode n6 = new BinaryTreeNode(6);
        BinaryTreeNode n7 = new BinaryTreeNode(7);
        BinaryTreeNode n8 = new BinaryTreeNode(8);

        n1.left = n2;
        n1.right = n3;
        n2.left = n4;
        n3.right = n5;
        n4.left = n6;


        System.out.println(height(n1));
    }
}
